/*
 * main.c
 *
 *  Created on: Apr 6, 2015
 *      Author: mxw
 */
#include <KeyDrv.h>
#include "LedDrv.h"
#include "UartDrv.h"
#include <eventQueue.h>
#include <timer.h>
#include <PmwDrv.h>

QueMgr g_Main_Queue;
unsigned char charmap[256] = {0};

void new_muisc_go(unsigned char music_idx);
extern void request_to_stop();

void exec_cmd(Queue_Element* pe) {
    switch (pe->cmd) {
    case CMD_UART_RECEVEI_DONE:
        if ('\n' == pe->data[0] || '\r' == pe->data[0]) {
            uart_print("\r\n");
        } else {
            uart_print_char(pe->data[0]);
        }

        if ('1' == pe->data[0]) {
            set_led(LED_ALL, LED_OFF);
            set_led(LED_1, LED_ON);
            uart_print("\r\nLED1 is on!\r\n");

            pmw_set_freq(1490);
            pmw_start_sing();

        } else if ('2' == pe->data[0]) {
            set_led(LED_ALL, LED_OFF);
            set_led(LED_2, LED_ON);
            uart_print("\r\nLED2 is on!\r\n");
            pmw_set_freq(1365);
            pmw_start_sing();

        } else if ('3' == pe->data[0]) {
            set_led(LED_ALL, LED_OFF);
            set_led(LED_3, LED_ON);
            uart_print("\r\nLED3 is on!\r\n");
            pmw_set_freq(1183);
            pmw_start_sing();
        } else if ('4' == pe->data[0]) {
            set_led(LED_ALL, LED_OFF);
            set_led(LED_4, LED_ON);
            uart_print("\r\nLED4 is on!\r\n");
            pmw_set_freq(1119);
            pmw_start_sing();
        } else if ('5' == pe->data[0]) {
            set_led(LED_ALL, LED_OFF);
            set_led(LED_1, LED_ON);
            set_led(LED_3, LED_ON);
            uart_print("\r\nLED13 is on!\r\n");
            pmw_set_freq(996);
            pmw_start_sing();
        } else if ('6' == pe->data[0]) {
            set_led(LED_ALL, LED_OFF);
            set_led(LED_2, LED_ON);
            set_led(LED_3, LED_ON);
            uart_print("\r\nLED23 is on!\r\n");
            pmw_set_freq(887);
            pmw_start_sing();
        } else if ('7' == pe->data[0]) {
            set_led(LED_ALL, LED_OFF);
            set_led(LED_1, LED_ON);
            set_led(LED_2, LED_ON);
            set_led(LED_3, LED_ON);
            uart_print("\r\nLED123 is on!\r\n");
            pmw_set_freq(790);
            pmw_start_sing();
        } else if ('m' == pe->data[0]) {
            new_muisc_go(0);
        } else if ('s' == pe->data[0]) {
            request_to_stop();
        } else if ('p' == pe->data[0]) {
            pmw_stop_sing();
        }
        break;
    case CMD_KEY1_DOWN:
        key1_down();
        break;
    case CMD_KEY2_DOWN:
        key2_down();
        break;
    case CMD_KEY3_DOWN:
        key3_down();
        break;
    case CMD_KEY4_DOWN:
        key4_down();
        break;
    case CMD_TIMER_TIMEOUT:
        call_timer_func(pe->data[0]);
        break;
    default:
        break;
    }
}

void on_main_timer_test(unsigned short tid) {
    //uart_print("__good!____\r\n");
}
extern void init_music();
int main() {
    Queue_Element* pe = 0;
    int i = 0;
    unsigned short tid = 0;

    for(i = 0; i < 256; ++i) {
        charmap[i] = i;
    }
    init_timer();
    init_key();
    init_led();
    uart_init(115200);
    init_event_queue(&g_Main_Queue);
    set_led(LED_1, LED_ON);
    init_pmw();
    init_music();


    uart_print("hello world!\r\n");
    uart_print("你好!好\r\n");
    uart_print("こんにちは、しばらくお待ちください。\r\n");

    tid = get_new_timer(on_main_timer_test);
    if (ERR_TIMER_ID == tid) {
        uart_print("get test_Timer Failed!\r\n");
    } else {
        uart_print("get test_Timer success!\r\n");
    }
    start_timer(tid, 5000); //5s timer

    while(1) {
        pe = pick_msg(&g_Main_Queue);
        while(pe) {
            exec_cmd(pe);
            pe = pick_msg(&g_Main_Queue);
        }
    }
    return 0;
}

































